/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ScrollPane.java
 *
 * Created on 24-jul-2010, 21:37:13
 */
package facturasapp;

import facturasapp.data.DataManager;
import facturasapp.data.StoreManager;
import facturasapp.events.FacturaAdapter;
import facturasapp.events.FacturaEvent;
import facturasapp.events.EventManager;
import facturasapp.events.FacturaListener;
import facturasapp.events.SearchAdapter;
import facturasapp.events.SearchEvent;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneLayout;
import storeDataModel.Invoice;
import storeDataModel.Product;
import storeDataModel.ProductRelation;

/**
 *
 * @author CiberXtrem
 */
public class SearchShoppingCartPane extends javax.swing.JScrollPane {

    /** Creates new form ScrollPane */
    public SearchShoppingCartPane() {
        //initComponents();
        initCustomComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setName("Form"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getViewport());
        getViewport().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    public void initCustomComponents() {

        this.setLayout(new ScrollPaneLayout());
        //this.setBounds(new Rectangle(0, 0, 400, 100));
        this.setPreferredSize(new java.awt.Dimension(200, 300));
        //this.setMinimumSize((new java.awt.Dimension(200, 400)));
        this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        //this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); //don't
        //this.setBorder(null);

        GridLayout gridLayout = new GridLayout(99, 0);
        panel.setLayout(gridLayout);

        this.getViewport().add(panel);


        //AddListener//
        EventManager.getInstance().addSearchListener(new SearchAdapter() {

            @Override
            public void invoiceUnselected(SearchEvent e) {
                System.out.println("S Shopping Cart Pane InvoiceUnselected");
                //panel = new JPanel(new GridLayout(99, 0));
                panel.removeAll();
                panel.revalidate();
                panel.repaint();
            }

            @Override
            public void invoiceSelected(SearchEvent e) {
                //DataValues
                Invoice invoice = (Invoice) e.getSource();
               
                ArrayList<ProductRelation> rels = invoice.getProductRelations();
                System.out.println("S Shopping Cart Pane Invoiceselected, items in cesta: " + rels.size());
                Iterator it = rels.iterator();
                while (it.hasNext()) {
                    //Obtenemos el producto desde source del evento
                    ProductRelation productRelation = (ProductRelation) it.next();

                    //agregamos a la lista de la compra visual la nueva ProductRelation y le pasamos el productRelation data para que lo pueda modificar
                    SearchProductShoppingCartPane productShoppingCartPane = new SearchProductShoppingCartPane();
                    //Setting values to produdShopPane
                    productShoppingCartPane.codigoField.setText(productRelation.getProducto().getCodigo());
                    productShoppingCartPane.descripcionField.setText(productRelation.getDescripcion());
                    productShoppingCartPane.precioField.setText(String.valueOf(productRelation.getPrecio()));

                    productShoppingCartPane.importeField.setText(String.valueOf(productRelation.getImporte()));
                    productShoppingCartPane.cantidadField.setText(String.valueOf(productRelation.getCantidad()));
                    
                    panel.add(productShoppingCartPane);
                }
               //end factura de la base de datos
                panel.revalidate();
                panel.repaint();

            }
            @Override
            public void newSearch (SearchEvent e){
                //Reset when starting a newSearch
                panel.removeAll();
                panel.revalidate();
                panel.repaint();

            }
                
        });
        //End AddListener//
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    //Custom Variables
    JPanel panel = new JPanel();
    //End Custom Variables
}
